"""Main entrypoint for preprocessor commands."""

import os
import argparse
import logging
import sys
import pickle
import vectorizer.ast2vec.train as ast2vec
from vectorizer.node_map import NODE_LIST

LOG_FILE = 'vectorizer/vectorizer.log'

def main():
    """Execute the preprocessor commandline interface."""

    if not os.path.isdir('vectorizer/logs/'):
        os.makedirs('vectorizer/logs/', 0o755)

    logging.getLogger().setLevel(logging.DEBUG)
    # log output to a file
    file_handler = logging.FileHandler(LOG_FILE, mode='w')
    file_handler.setLevel(logging.DEBUG)
    # log output to stdout
    stdout_handler = logging.StreamHandler(sys.stdout)
    stdout_handler.setLevel(logging.DEBUG)

    logging.getLogger().addHandler(stdout_handler)
    logging.getLogger().addHandler(file_handler)

    parser = argparse.ArgumentParser(
        description="Vectorize a node sample file generated by the sampler",
    )

    parser.add_argument(
        'model',
        type=str,
        help='Model to train. Available options are: ast2vec'
    )

    parser.add_argument(
        '--in',
        dest='infile',
        type=str,
        help='Input sample file to use',
    )

    parser.add_argument(
        '--out',
        dest='outfile',
        type=str,
        help='Output file to store the embeddings'
    )

    parser.add_argument(
        '--checkpoint',
        type=str,
        help='Directory to store checkpoints for TensorFlow'
    )

    args = parser.parse_args()

    with open(args.infile, 'rb') as sample_file:
        samples = pickle.load(sample_file)

    if args.model.lower() == 'ast2vec':
        ast2vec.learn_vectors(samples, args.checkpoint, args.outfile)
